package cn.linkai.star.graph.core.config;

import cn.linkai.star.graph.core.interceptor.WebsocketUserInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.ChannelRegistration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
    // 配置WebSocket端点，添加HTTP会话拦截器，并允许所有来源的跨域请求
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint ("/ws")
                .addInterceptors (new HttpSessionHandshakeInterceptor ())
                .setAllowedOriginPatterns ("*");
    }

    /**
     * 配置消息代理,消息路由
     * @param registry
     */
    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableSimpleBroker ("/topic","/user");
    }

    /**
     * 配置客户端入站通道拦截器
     * @param registration
     */
    @Override
    public void configureClientInboundChannel(ChannelRegistration registration) {
        registration.interceptors (new WebsocketUserInterceptor ());
    }
}
